package cn.dckeji.dcssm.common.utils;


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @since 1.0
 * ${tags}
 * <br><b>作者： @author WangDong</b>
 * <br>创建时间：2018/2/22 0022 9:27
 */
public class StringUtil {


    public static String  getUUId(){
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-","");
    }

    public static Map<String,Object> getFileName(String fileName,String filePath,MultipartFile file) throws IOException {
        String random = getRandom(6);
        String name = file.getOriginalFilename();
        //InputStream inputStream = file.getInputStream();
        //扩展名
        String suffix = name.substring(name.lastIndexOf("."));
        LinkedHashMap<String,Object> map = new LinkedHashMap<>();
       // map.put(PublicCode.INPUT_STREAM,inputStream);
        map.put(fileName,name);
        map.put(filePath,DateUtils.getTimeCode()+random+suffix);
        return map;
    }

    public static String  getRandom(int length){
        Random random = new Random();
        StringBuffer str = new StringBuffer();
        for (int i=0;i<length;i++)
        {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }

    public static String oldUrl(String oldUrl){
        String subStr = oldUrl.substring(oldUrl.lastIndexOf("/")+1,oldUrl.length());
        return subStr;
    }

    public static String orderNo(int length){
        StringBuffer sb = new StringBuffer();
        sb.append(DateUtils.getTimeCodeHour());
        sb.append(getRandom(length));
        return sb.toString();
    }

    public static String qiNiuUrl(int length){
        StringBuffer sb = new StringBuffer();
        sb.append(DateUtils.getTimeCode());
        sb.append(getRandom(length));
        return sb.toString();
    }

    public static void main(String[] args) {
        String userId="7a9f193c707e47afa0e1e052d44b56df";
        String chars = "AbCDEFGHIJKLMNOPQRESTUVWXYZ";
            StringBuffer sb=new StringBuffer();
            sb.append(chars.charAt((int) (Math.random() * 26)));
            sb.append(chars.charAt((int) (Math.random() * 26)));
            sb.append(chars.charAt((int) (Math.random() * 26)));
            sb.append(chars.charAt((int) (Math.random() * 26)));
            sb.append(new Date().getTime()/60000);
            sb.append(userId.substring(userId.length() - 4, userId.length()));
            System.out.println(sb.toString());
    }

    public static List<String>  toList(String[]... split){
        StringBuffer str = new StringBuffer();
        for(int i=0; i<split.length; i++){
            if(i!=0){
                str.append(",");
            }
            str.append(split[i].toString());
        }
        List<String> strings = Arrays.asList(str.toString().split(","));
        return strings;
    }

    /**
     * // 入库之前，使用 Base64 编码
     * @param nickName
     * @return
     * @throws UnsupportedEncodingException
     */
    public static  String  base64NikeName(String nickName) throws UnsupportedEncodingException{
        // 入库之前，使用 Base64 编码
        return Base64.encodeBase64String(nickName.getBytes("utf-8"));
    }

    /**
     * // 出库后，使用 Base64 解码
     * @param nickName
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String base64DecodeNikeName(String nickName)throws UnsupportedEncodingException{
        return new String(Base64.decodeBase64(nickName.getBytes()), "utf-8");
    }

    public static String toUTF8(String str) throws UnsupportedEncodingException{
        if(StringUtils.isEmpty(str)){
            return "";
        }
        return new String(str.getBytes("iso-8859-1"),"UTF-8");
    }

}
